package com.cpimex.eprice.action;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import javax.naming.*;

import java.sql.*;
import com.cpimex.eprice.action.plugin.ConnectionBuilder;
import com.cpimex.eprice.forms.LoginActionForm;
import com.cpimex.eprice.model.Permisos;
import com.cpimex.eprice.model.users.Usuario;

public class LoginAction extends Action {
    public ActionForward execute(ActionMapping actionMapping,
                                 ActionForm actionForm,
                                 HttpServletRequest servletRequest,
                                 HttpServletResponse servletResponse) throws
            Exception {
        int statusLogin = -1;
        System.out.println("entro verificar");
        InitialContext context = new InitialContext();
        //Connection conn_modseg = ConnectionBuilder.msConnection(context);//ds_modSeg.getConnection();
        DataSource ds_modSeg = (DataSource) context.lookup("java:comp/env/jdbc/ModSeg");
    	Connection conn_modseg = ds_modSeg.getConnection();
        
        //Connection conn_ePrice = ConnectionBuilder.epriceConnection(context);//ds_ePrice.getConnection();
    	DataSource dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/Eprice");
    	Connection conn_ePrice = dataSrc.getConnection();
    	
        System.out.println("paso conexion");
        try {

            LoginActionForm loginActionForm = (LoginActionForm) actionForm;
            String str_usuario = loginActionForm.getStr_usuario();
            String str_password = loginActionForm.getStr_password();

            HttpSession session = servletRequest.getSession();

            statusLogin = Login.valida(conn_modseg, str_usuario, str_password);

            switch (statusLogin) {
            case -1: {
                servletRequest.setAttribute(
                        "Double.parseDouble(bn_producto.getStr_precioClientesProductos());app",
                        "Usuario Invalido");
                return actionMapping.findForward("login");
            }
            case 0: {
            	System.out.println("entro verificar perfil");
            	Usuario us = Usuario.obtenPerfil(conn_ePrice, str_usuario);
            	System.out.println("paso regresar usuario");
            	
            	
            	session.setAttribute("usuario", us);
                session.setAttribute("usuario_app", Login.usuario);
                session.setAttribute("permisos_app", Permisos.permisosApp(Login.usuario));
                //System.out.println(session.getAttribute("permisos_app"));
                //String a = (String) session.getAttribute("permisos_app");
                return actionMapping.findForward("success");
            }
            case 1: {
                servletRequest.setAttribute("app", "Usuario invalido para la aplicacion");
                return actionMapping.findForward("login");
            }
            default: {
                return actionMapping.findForward("login");
            }
            }

        } catch (Exception ex) {
            throw ex;
        } finally {
            conn_modseg.close();
            conn_ePrice.close();
        }
    }
}
